<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css"/>




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  




<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="JavaScript迭代优化," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="用减值迭代，或用变量len代替list.length，可简化每次循环对list.length的访问12345678910var i,	len,	item,	list=document.getElementsByTagName(&apos;li&apos;);	for(i=0,len=list.length;i &amp;lt;len;i++)&amp;#123;		item=list[i];		item.addEventListen">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript中HTMLCollection迭代优化">
<meta property="og:url" content="http://ycdo.github.com/2016/06/13/JavaScript中HTMLCollection迭代优化/index.html">
<meta property="og:site_name" content="勤能补拙">
<meta property="og:description" content="用减值迭代，或用变量len代替list.length，可简化每次循环对list.length的访问12345678910var i,	len,	item,	list=document.getElementsByTagName(&apos;li&apos;);	for(i=0,len=list.length;i &amp;lt;len;i++)&amp;#123;		item=list[i];		item.addEventListen">
<meta property="og:updated_time" content="2016-06-13T14:03:21.172Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaScript中HTMLCollection迭代优化">
<meta name="twitter:description" content="用减值迭代，或用变量len代替list.length，可简化每次循环对list.length的访问12345678910var i,	len,	item,	list=document.getElementsByTagName(&apos;li&apos;);	for(i=0,len=list.length;i &amp;lt;len;i++)&amp;#123;		item=list[i];		item.addEventListen">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 6295473888616777000,
      author: 'YCDO'
    }
  };
</script>

  <title> JavaScript中HTMLCollection迭代优化 | 勤能补拙 </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">勤能补拙</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">工欲善其事，必先利其器</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-home fa-fw"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-archive fa-fw"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-tags fa-fw"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-th fa-fw"></i> <br />
            
            分类
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                JavaScript中HTMLCollection迭代优化
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-06-13T07:25:00+08:00" content="2016-06-13">
              2016-06-13
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/JavaScript/" itemprop="url" rel="index">
                    <span itemprop="name">JavaScript</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2016/06/13/JavaScript中HTMLCollection迭代优化/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/06/13/JavaScript中HTMLCollection迭代优化/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="用减值迭代，或用变量len代替list-length，可简化每次循环对list-length的访问"><a href="#用减值迭代，或用变量len代替list-length，可简化每次循环对list-length的访问" class="headerlink" title="用减值迭代，或用变量len代替list.length，可简化每次循环对list.length的访问"></a>用减值迭代，或用变量len代替list.length，可简化每次循环对list.length的访问</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> i,</span><br><span class="line">	len,</span><br><span class="line">	item,</span><br><span class="line">	list=<span class="built_in">document</span>.getElementsByTagName(<span class="string">'li'</span>);</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>,len=list.length;i &lt;len;i++)&#123;</span><br><span class="line">		item=list[i];</span><br><span class="line">		item.addEventListener(<span class="string">'click'</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">			<span class="built_in">console</span>.log();</span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<h3 id="经典的duff循环（进行优化后的）"><a href="#经典的duff循环（进行优化后的）" class="headerlink" title="经典的duff循环（进行优化后的）"></a>经典的duff循环（进行优化后的）</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> func=<span class="function"><span class="keyword">function</span>(<span class="params">array,handle</span>)</span>&#123;</span><br><span class="line">	<span class="keyword">var</span> length=array.length,</span><br><span class="line">		index=<span class="number">0</span>,</span><br><span class="line">		remainder=length%<span class="number">8</span>,</span><br><span class="line">		times=<span class="built_in">Math</span>.door(length/<span class="number">8</span>);</span><br><span class="line">	<span class="comment">///余数循环</span></span><br><span class="line">	<span class="keyword">while</span>(remainder--&gt;<span class="number">0</span>)&#123;</span><br><span class="line">		handle(array[index++],index<span class="number">-1</span>,array);</span><br><span class="line">		&#125;</span><br><span class="line">	<span class="comment">///主循环</span></span><br><span class="line">	<span class="keyword">while</span>(times--&gt;<span class="number">0</span>)&#123;</span><br><span class="line">		handle(array[index++],index<span class="number">-1</span>,array);</span><br><span class="line">		handle(array[index++],index<span class="number">-1</span>,array);</span><br><span class="line">		handle(array[index++],index<span class="number">-1</span>,array);</span><br><span class="line">		handle(array[index++],index<span class="number">-1</span>,array);</span><br><span class="line">		handle(array[index++],index<span class="number">-1</span>,array);</span><br><span class="line">		handle(array[index++],index<span class="number">-1</span>,array);</span><br><span class="line">		handle(array[index++],index<span class="number">-1</span>,array);</span><br><span class="line">		handle(array[index++],index<span class="number">-1</span>,array);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">item,index,array</span>)</span>&#123;</span><br><span class="line">		<span class="comment">///其他代码</span></span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
      
    </div>
    
    <div>
      
        
      
    </div>

    <div>
      
        
      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/JavaScript迭代优化/" rel="tag">#JavaScript迭代优化</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/05/01/搭建属于你的个人博客/" rel="next" title="搭建属于你的个人博客">
                <i class="fa fa-chevron-left"></i> 搭建属于你的个人博客
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/09/21/Mvc5入门学习之MVC模式/" rel="prev" title="Mvc5入门学习之MVC模式">
                Mvc5入门学习之MVC模式 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <div class="ds-share flat" data-thread-key="2016/06/13/JavaScript中HTMLCollection迭代优化/"
     data-title="JavaScript中HTMLCollection迭代优化"
     data-content=""
     data-url="http://ycdo.github.com/2016/06/13/JavaScript中HTMLCollection迭代优化/">
  <div class="ds-share-inline">
    <ul  class="ds-share-icons-16">

      <li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
      <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
      <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
      <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
      <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>

    </ul>
    <div class="ds-share-icons-more">
    </div>
  </div>
</div>
      
    </div>
  </div>


          </div>
          

  <p>热评文章</p>
  <div class="ds-top-threads" data-range="weekly" data-num-items="4"></div>


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/06/13/JavaScript中HTMLCollection迭代优化/"
           data-title="JavaScript中HTMLCollection迭代优化" data-url="http://ycdo.github.com/2016/06/13/JavaScript中HTMLCollection迭代优化/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://zhangbaolei.com/images/avatar2.png"
               alt="张宝蕾" />
          <p class="site-author-name" itemprop="name">张宝蕾</p>
          <p class="site-description motion-element" itemprop="description">工欲善其事，必先利其器</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">8</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#用减值迭代，或用变量len代替list-length，可简化每次循环对list-length的访问"><span class="nav-number">1.</span> <span class="nav-text">用减值迭代，或用变量len代替list.length，可简化每次循环对list.length的访问</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#经典的duff循环（进行优化后的）"><span class="nav-number">2.</span> <span class="nav-text">经典的duff循环（进行优化后的）</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">张宝蕾</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"ycdo"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
      
      <script src="/vendors/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
      <script src="/js/src/hook-duoshuo.js"></script>
    
  





  
  
  

  

  

</body>
</html>
